<template>
<div class="app">
  <el-form :model="user" status-icon :rules="rules" ref="user" label-position="left" label-width="71px" size="medium">
    <el-form-item label="密码" prop="password">
      <el-input type="password" v-model="user.password" autocomplete="off"  show-password clearable></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPass">
      <el-input type="password" v-model="user.checkPass" autocomplete="off"  show-password clearable></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="danger" @click="submitForm('user')">修改密码</el-button>
      <el-button @click="resetForm('user')">重置</el-button>
    </el-form-item>
  </el-form>
  <el-alert
      class="el-alert"
      title="修改密码后会立刻退出登录"
      type="warning">
  </el-alert>
</div>
</template>

<script>
import {updateUser} from "@/utils/api";
import router from "@/router";

export default {
  name: "UploadPasswordComponent",
  props: {},
  data() {

    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.user.checkPass !== '') {
          this.$refs.user.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.user.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      user: {
        password: '',
        checkPass: '',
        userName: '',
        userImg: "",
        nickName: "",
      },
      rules: {
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  }, methods: {
    Logout() {
      router.push("/Login")
      window.localStorage.removeItem('token');
      window.localStorage.removeItem('user');
      this.$store.commit("setUser",Object)
      this.$message.success('已退出登录')
      // this.$forceUpdate()
    },
    update(){
      // console.log(this.user)
      const user = this.getUser;
      updateUser(
          user
      ).then(res=>{
        if(res.code === 200){
          this.$notify.success("提交成功")
          // console.log(res)

          this.Logout()
        }else{
          this.$notify.error("提交失败")
          // console.error(res)
        }
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.password = this.user.password
          this.update()
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }

  }, watch: {}, computed: {
    getUser:{
      get(){
        return this.$store.getters.getUser;
      },
      set(value){
        return this.$store.commit("setUser",value);
      }
    },
    password:{
      get(){
        return '';
      },
      set(value){
        var user = this.getUser
        user.password = value
        return this.$store.commit("setUser",user);
      }
    }
  }
}
</script>

<style scoped>
.app{
  width: 500px;
}
>>>.el-alert{
  line-height: 18px;
}
</style>